#include <vector>
#include <string>
#include <unordered_map>
#include <iostream>

using namespace std;

class Solution 
{
public:
    vector<int> findAnagrams(string s, string p) 
    {
        vector<int> result;
        if(s.length() == 0 || p.length() == 0 || s.length() < p.length())
        {
            return result;
        }
        unordered_map<char, int> um;
        for(int i = 0; i < p.length(); i++)
        {
            um[p[i]]++;
        }
        unordered_map<char, int> um2;
        for(int i = 0; i < p.length(); i++)
        {
            um2[s[i]]++;
            if(um == um2)
            {
                result.push_back(0);
            }
        }
        for(int i = p.length(); i < s.length(); i++)
        {
            um2[s[i - p.length()]]--;
            if(um2[s[i - p.length()]] == 0)
            {
                um2.erase(s[i - p.length()]);
            }
            um2[s[i]]++;
            if(um == um2)
            {
                result.push_back(i - p.length() + 1);
            }
        }
        return result;
    }
};

// 辅助函数：打印结果
void printResult(const vector<int>& result) 
{
    cout << "[";
    for(int i = 0; i < result.size(); i++) 
    {
        cout << result[i];
        if(i < result.size() - 1) cout << ",";
    }
    cout << "]" << endl;
}

int main() 
{
    Solution solution;
    
    // 测试案例1: s = "cbaebabacd", p = "abc"
    cout << "测试案例1:" << endl;
    string s1 = "cbaebabacd";
    string p1 = "abc";
    cout << "输入: s = \"" << s1 << "\", p = \"" << p1 << "\"" << endl;
    vector<int> result1 = solution.findAnagrams(s1, p1);
    cout << "输出: ";
    printResult(result1);
    cout << "期望: [0,6]" << endl << endl;
    
    // 测试案例2: s = "abab", p = "ab"
    cout << "测试案例2:" << endl;
    string s2 = "abab";
    string p2 = "ab";
    cout << "输入: s = \"" << s2 << "\", p = \"" << p2 << "\"" << endl;
    vector<int> result2 = solution.findAnagrams(s2, p2);
    cout << "输出: ";
    printResult(result2);
    cout << "期望: [0,1,2]" << endl << endl;
    
    // 测试案例3: s长度小于p
    cout << "测试案例3 (s长度小于p):" << endl;
    string s3 = "a";
    string p3 = "ab";
    cout << "输入: s = \"" << s3 << "\", p = \"" << p3 << "\"" << endl;
    vector<int> result3 = solution.findAnagrams(s3, p3);
    cout << "输出: ";
    printResult(result3);
    cout << "期望: []" << endl << endl;
    
    // 测试案例4: 相同字符串
    cout << "测试案例4 (相同字符串):" << endl;
    string s4 = "abc";
    string p4 = "abc";
    cout << "输入: s = \"" << s4 << "\", p = \"" << p4 << "\"" << endl;
    vector<int> result4 = solution.findAnagrams(s4, p4);
    cout << "输出: ";
    printResult(result4);
    cout << "期望: [0]" << endl << endl;
    
    // 测试案例5: 无匹配
    cout << "测试案例5 (无匹配):" << endl;
    string s5 = "abcd";
    string p5 = "ef";
    cout << "输入: s = \"" << s5 << "\", p = \"" << p5 << "\"" << endl;
    vector<int> result5 = solution.findAnagrams(s5, p5);
    cout << "输出: ";
    printResult(result5);
    cout << "期望: []" << endl << endl;
    
    // 测试案例6: 单字符重复
    cout << "测试案例6 (单字符重复):" << endl;
    string s6 = "aaaa";
    string p6 = "aa";
    cout << "输入: s = \"" << s6 << "\", p = \"" << p6 << "\"" << endl;
    vector<int> result6 = solution.findAnagrams(s6, p6);
    cout << "输出: ";
    printResult(result6);
    cout << "期望: [0,1,2]" << endl << endl;
    
    // 测试案例7: 空字符串
    cout << "测试案例7 (空字符串):" << endl;
    string s7 = "";
    string p7 = "a";
    cout << "输入: s = \"" << s7 << "\", p = \"" << p7 << "\"" << endl;
    vector<int> result7 = solution.findAnagrams(s7, p7);
    cout << "输出: ";
    printResult(result7);
    cout << "期望: []" << endl << endl;
    
    // 测试案例8: 长字符串
    cout << "测试案例8 (长字符串):" << endl;
    string s8 = "bpbpbpbpbp";
    string p8 = "bp";
    cout << "输入: s = \"" << s8 << "\", p = \"" << p8 << "\"" << endl;
    vector<int> result8 = solution.findAnagrams(s8, p8);
    cout << "输出: ";
    printResult(result8);
    cout << "期望: [0,2,4,6,8]" << endl << endl;
    
    // 测试案例9: 复杂异位词
    cout << "测试案例9 (复杂异位词):" << endl;
    string s9 = "abaacbabc";
    string p9 = "abc";
    cout << "输入: s = \"" << s9 << "\", p = \"" << p9 << "\"" << endl;
    vector<int> result9 = solution.findAnagrams(s9, p9);
    cout << "输出: ";
    printResult(result9);
    cout << "期望: [3,4,6]" << endl << endl;
    
    // 测试案例10: 单字符
    cout << "测试案例10 (单字符):" << endl;
    string s10 = "a";
    string p10 = "a";
    cout << "输入: s = \"" << s10 << "\", p = \"" << p10 << "\"" << endl;
    vector<int> result10 = solution.findAnagrams(s10, p10);
    cout << "输出: ";
    printResult(result10);
    cout << "期望: [0]" << endl << endl;
    
    return 0;
}